<?php 
namespace PsiFramework\Classes\Util\Parameters\DAO;

import("Classes.Util.Application");
import("Classes.Util.Manager.PluginManager");
import("Classes.Util.UI.Module");
import("Classes.Util.Parameters.DAO.ConfigDAO");

use \PsiFramework\Classes\Util\Application;
use \PsiFramework\Classes\Util\Manager\PluginManager;
use \PsiFramework\Classes\Util\UI\Module;
use \PsiFramework\Classes\Util\Parameters\DAO\ConfigDAO;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The ApplicationDAO class is the generic class to
 * retrieve application parameters
 *
 * @package Util.Parameters.DAO
 */
class ApplicationDAO extends ConfigDAO
{

    public function retrieveApplication()
    {

        $vars = $this->retrieve();

        //Default Action
        $defaultAction = $this->_racine
            ->getElementsByTagName("defaultAction")
            ->item(0)->nodeValue ;

        //securityCheck
        $sSecurityCheck = $this->_racine
            ->getElementsByTagName("securityCheck")
            ->item(0)->nodeValue ;
        $securityCheck = (strtoupper($sSecurityCheck)=='FALSE')?false:true ;

        //Default Language
        $defaultLanguage = $this->_racine
            ->getElementsByTagName("defaultLanguage")
            ->item(0)->nodeValue ;

        //Contact
        $contact = $this->_racine
            ->getElementsByTagName("contact")
            ->item(0)->nodeValue ;

        //Modules
        $modules = array();
        $modulesElement = $this->_racine
            ->getElementsByTagName("modules")
            ->item(0);
        if (!is_null($modulesElement)) {
            $moduleElements = $modulesElement->getElementsByTagName("module");
            foreach ($moduleElements as $moduleElement) {
                $moduleName = $moduleElement->getAttribute("name");
                $moduleTemplate = $moduleElement->getAttribute("template");
                $module = new Module($moduleName, $moduleTemplate);
                $modules[$moduleName] = $module ;
            }
        }

        //plugins
        $pluginManager = PluginManager::getInstance();
        $plugins = array();
        $pluginsElement = $this->_racine
            ->getElementsByTagName("plugins")
            ->item(0);
        if (!is_null($pluginsElement)) {
            $pluginsElements = $pluginsElement->getElementsByTagName("plugin");
            foreach ($pluginsElements as $pluginElement) {
                $pluginName = $pluginElement->getAttribute("name");
                $plugin = $pluginManager->getPlugin($pluginName);
                if (!is_null($plugin)) {
                    $plugins[] = $plugin ;
                }
            }
        }

        $application = new Application();
        $application->setName($vars['name']);
        $application->setDefaultAction($defaultAction);
        $application->setDefaultLanguage($defaultLanguage);
        $application->setContact($contact);
        $application->setPlugins($plugins);
        $application->setModules($modules);
        $application->setActionMapping($vars['actionMapping']);
        $application->setServiceMapping($vars['servicesParameters']);
        $application->setParameters($vars['parameters']);
        $application->setObjectParameters($vars['objectParameters']);
        $application->setSecurityCheck($securityCheck);

        return $application ;
    }
}
